﻿using System.Data;
using System;
using MySql.Data.Types;
using System.Globalization;
namespace Reviewer.Utilities.Data
{
    /// <summary>
    /// 
    /// </summary>
    public static class DataReaderUtility
    {
        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="ordinal">The ordinal.</param>
        /// <param name="returnValueIfDBNull">The return value if DB null.</param>
        /// <returns></returns>
        public static string GetValue(IDataRecord reader, int ordinal, string returnValueIfDBNull)
        {
            if (reader == null)
            {
                return returnValueIfDBNull;
            }

            return reader.IsDBNull(ordinal) ? returnValueIfDBNull : reader.GetString(ordinal);
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="ordinal">The ordinal.</param>
        /// <param name="returnValueIfDBNull">The return value if DB null.</param>
        /// <returns></returns>
        public static Int32 GetValue(IDataRecord reader, int ordinal, Int32 returnValueIfDBNull)
        {
            if (reader == null)
            {
                return returnValueIfDBNull;
            }

            try
            {
                return reader.IsDBNull(ordinal) ? returnValueIfDBNull : reader.GetInt32(ordinal);
            }
            catch (FormatException) { }

            return returnValueIfDBNull;
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="ordinal">The ordinal.</param>
        /// <param name="returnValueIfDBNull">The return value if DB null.</param>
        /// <returns></returns>
        public static Int32? GetValue(IDataRecord reader, int ordinal, Int32? returnValueIfDBNull)
        {
            if (reader == null)
            {
                return returnValueIfDBNull;
            }

            try
            {
                return (Int32?)(reader.IsDBNull(ordinal) ? returnValueIfDBNull : reader.GetInt32(ordinal));
            }
            catch (FormatException)
            {
                return returnValueIfDBNull;
            }
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="ordinal">The ordinal.</param>
        /// <param name="returnValueIfDBNull">if set to <c>true</c> [return value if DB null].</param>
        /// <returns></returns>
        public static Boolean GetValue(IDataRecord reader, int ordinal, bool returnValueIfDBNull)
        {
            if (reader != null)
                return reader.IsDBNull(ordinal) ? returnValueIfDBNull : reader.GetBoolean(ordinal);

            return returnValueIfDBNull;
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="ordinal">The ordinal.</param>
        /// <param name="returnValueIfDBNull">The return value if DB null.</param>
        /// <returns></returns>
        public static DateTime GetValue(IDataRecord reader, int ordinal, DateTime returnValueIfDBNull)
        {
            if (reader == null)
            {
                return returnValueIfDBNull;
            }

            //try catch to solve invlid timestamp issue
            try
            {
                if (!reader.IsDBNull(ordinal))
                {
                    if (!reader[ordinal].ToString().EndsWith("0000", StringComparison.Ordinal))
                    {
                        try
                        {
                            return reader.GetDateTime(ordinal);
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            return DateTime.Parse(reader.GetString(ordinal), CultureInfo.InvariantCulture);
                        }
                    }
                }
            }
            catch (MySqlConversionException) { }
            return returnValueIfDBNull;
        }

        /// <summary>
        /// Gets the value.
        /// </summary>
        /// <param name="reader">The reader.</param>
        /// <param name="ordinal">The ordinal.</param>
        /// <param name="returnValueIfDBNull">The return value if DB null.</param>
        /// <returns></returns>
        public static Guid GetValue(IDataRecord reader, int ordinal, Guid returnValueIfDBNull)
        {
            if (reader == null)
            {
                return returnValueIfDBNull;
            }

            try
            {
                return reader.IsDBNull(ordinal) ? returnValueIfDBNull : reader.GetGuid(ordinal);
            }
            catch (FormatException)
            {
                System.Diagnostics.Debug.WriteLine(reader.GetValue(ordinal));
            }
            return returnValueIfDBNull;
        }
    }
}